package cn.edu.zut.hhr.entity;

import cn.edu.zut.hhr.enums.IddentityEnum;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SecurityUser implements UserDetails,Serializable {

    User user;

    public SecurityUser(User user) {
        this.user = user;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Set<String> getRoles(){
        Set<String> set = new HashSet<>();
        set.add(IddentityEnum.getByBoolean(user.getIddentity()).getIddentity());
        List<String> list = user.getRoles();
        set.addAll(list);
        return set;
    }

    @Override
    public Set<? extends GrantedAuthority> getAuthorities() {
        Set<GrantedAuthority> set = new HashSet<>();
        List<String> list = user.getRoles();
        set.add(new SimpleGrantedAuthority(user.getIddentity()?"customer":"STAFF"));
        for(String role: list){
            set.add(new SimpleGrantedAuthority(role.toUpperCase()));
        }
        return set;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getId().toString();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
